<?php


namespace app\wenchuang\controller;


use app\wenchuang\model\NoticeModel;
use app\wenchuang\validate\NoticeValidate;
use cmf\controller\AdminBaseController;

class NoticeController extends AdminBaseController
{
     public function index()
     {
         $notice = NoticeModel::order('list_order','desc')
         ->select()
         ->toArray();
         $this->assign('notice', $notice);
         return $this->fetch();
     }

    //排序
    public function listOrder()
    {
        parent::listOrders(new NoticeModel());
        $this->success('排序成功');
    }

     //显示添加页面
     public function add()
     {
         return $this->fetch();
     }
     //添加保存
     public function addpost()
     {
         $data = $this->request->param();

         $content = htmlspecialchars_decode($data['content']);
         $content = cmf_replace_content_file_url($content, true);
         $content = htmlspecialchars($content);
         $data['content'] = $content;
         $validate = new NoticeValidate();
         $result = $validate->scene('add')->check($data);
         if ($result !== true) {
            $this->error($validate->getError());
         }
        // 存库
        $bannerModel = New NoticeModel();
        if ($bannerModel->save($data)) {
            $this->success('添加成功', 'Notice/index');
        }
        $this->error('添加失败');
     }
     //显示修改页面
     public function edit()
     {
         $id = $this->request->param('id');
         $notice = NoticeModel::find($id);
         //显示编辑页面也要处理存在数据库的富文本
         $notice['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($notice['content']));
         $this->assign('notice', $notice);
         return $this->fetch();
     }
    // 编辑保存
    public function editPost()
    {
        $data = $this->request->param();
        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content, true);
        $content = htmlspecialchars($content);
        $data['content'] = $content;
        // 验证数据
        $validate = new NoticeValidate();
        $result = $validate->scene('add')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }

        // 编辑存库
        $NoticeModel = New NoticeModel();
        $Notice = $NoticeModel->where('id', '=', $data['id'])->find();
        $Notice->title = $data['title'];
        $Notice->abstract = $data['abstract'];
        $Notice->image = $data['image'];
        $Notice->content = $data['content'];
        $Notice->author = $data['author'];
        if ($Notice->save()) {
            $this->success('保存成功', 'Notice/index');
        }
        $this->error('保存失败');
    }

    // 删除
    public function softDelete()
    {
        $id = input('id');
        $workModel = New NoticeModel();
        // 查询要删除的数据
        $work = $workModel->where('id', '=', $id)->find();
        if ($work->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
}